home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / innd / proc.c < prev    next >
C/C++ Source or Header  |  1992-08-14  |  3KB  |  176 lines

  1. /*  $Revision: 1.11 $
  2. **
  3. **  Process control routines.
  4. */
  5. #include "innd.h"
  6. #include <signal.h>
  7.  
  8.  
  9. STATIC PROCESS    *PROCtable;
  10. STATIC int    PROCtablesize;
  11. STATIC PROCESS    PROCnull = { PSfree };
  12.  
  13.  
  14. /*
  15. **  Collect dead processes.
  16. */
  17. STATIC void
  18. PROCreap()
  19. {
  20.     int            status;
  21.     register PROCESS    *pp;
  22.     register int    i;
  23.     register int    pid;
  24.  
  25.     for ( ; ; ) {
  26.     pid = waitnb(&status);
  27.     if (pid == 0)
  28.         break;
  29.     if (pid < 0) {
  30.         if (errno != ECHILD)
  31.         syslog(L_ERROR, "%s cant wait %m", LogName);
  32.         break;
  33.     }
  34.     for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
  35.         if (pp->Pid == pid) {
  36.         PROCneedscan = TRUE;
  37.         pp->Status = status;
  38.         pp->State = PSdead;
  39.         pp->Collected = Now.time;
  40.         break;
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46. /*
  47. **  Signal handler that collects the processes, then resets the signal.
  48. */
  49. STATIC SIGHANDLER
  50. PROCcatchsignal(s)
  51.     int            s;
  52. {
  53.     PROCreap();
  54.     (void)signal(s, PROCcatchsignal);
  55. }
  56.  
  57.  
  58. /*
  59. **  Synchronous version that notifies a site when its process went away.
  60. */
  61. void
  62. PROCscan()
  63. {
  64.     register PROCESS    *pp;
  65.     register int    i;
  66.  
  67.     for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
  68.     if (pp->State == PSdead) {
  69.         if (pp->Site > 0)
  70.         SITEprocdied(&Sites[pp->Site], pp - PROCtable, pp);
  71.         pp->State = PSfree;
  72.     }
  73.     PROCneedscan = FALSE;
  74. }
  75.  
  76.  
  77. #if    0
  78. /*
  79. **  Close down all processes.
  80. */
  81. void
  82. PROCclose(Quickly)
  83.     BOOL        Quickly;
  84. {
  85.     register int    sig;
  86.     register PROCESS    *pp;
  87.     register int    i;
  88.  
  89.     /* What signal are we sending? */
  90.     sig = Quickly ? SIGKILL : SIGTERM;
  91.  
  92.     /* Send the signal to all living processes. */
  93.     for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++) {
  94.     if (pp->State != PSrunning)
  95.         continue;
  96.     if (kill(pp->Pid, sig) < 0 && errno != ESRCH)
  97.         syslog(L_ERROR, "%s cant kill %s %d %m",
  98.         LogName, Quickly ? "KILL" : "TERM", pp->Pid);
  99.     }
  100.  
  101.     /* Collect any who might have died. */
  102.     PROCreap();
  103.     for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
  104.     if (pp->State == PSdead)
  105.         *pp = PROCnull;
  106. }
  107. #endif    /* 0 */
  108.  
  109.  
  110. /*
  111. **  Stop watching a process -- we don't care about it any more.
  112. */
  113. void
  114. PROCunwatch(process)
  115.     int        process;
  116. {
  117.     if (process < 0 || process >= PROCtablesize) {
  118.     syslog(L_ERROR, "%s internal PROCunwatch %d", LogName, process);
  119.     return;
  120.     }
  121.     PROCtable[process].Site = -1;
  122. }
  123.  
  124.  
  125. /*
  126. **  Add a pid to the list of processes we watch.
  127. */
  128. int
  129. PROCwatch(pid, site)
  130.     int            pid;
  131.     int            site;
  132. {
  133.     register PROCESS    *pp;
  134.     register int    i;
  135.  
  136.     /* Find a free slot for this process. */
  137.     for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
  138.     if (pp->State == PSfree)
  139.         break;
  140.     if (i < 0) {
  141.     /* Ran out of room -- grow the table. */
  142.     RENEW(PROCtable, PROCESS, PROCtablesize + 20);
  143.     pp = &PROCtable[PROCtablesize];
  144.     PROCtablesize += 20;
  145.     }
  146.  
  147.     pp->State = PSrunning;
  148.     pp->Pid = pid;
  149.     pp->Started = Now.time;
  150.     pp->Site = site;
  151.     return pp - PROCtable;
  152. }
  153.  
  154.  
  155. /*
  156. **  Setup.
  157. */
  158. void
  159. PROCsetup(i)
  160.     register int    i;
  161. {
  162.     register PROCESS    *pp;
  163.  
  164.     if (PROCtable)
  165.     DISPOSE(PROCtable);
  166.     PROCtablesize = i;
  167.     PROCtable = NEW(PROCESS, PROCtablesize);
  168.     for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
  169.     *pp = PROCnull;
  170.  
  171. #if    defined(SIGCHLD)
  172.     (void)signal(SIGCHLD, PROCcatchsignal);
  173. #endif    /* defined(SIGCHLD) */
  174.     (void)signal(SIGPIPE, PROCcatchsignal);
  175. }
  176.